<?php
require_once dirname(__FILE__)."/../../outer/Log.php";
/**
 * 基础类 封装了db和logger
 * @author xucongbin
 *
 */
class base_model extends Model {

	protected $db;
    
	protected $logger;
    
	protected $errNo=0;
	protected $errMsg='';
	
	public function __construct() {
		parent::__construct();
	    if (!$this->db) {
			$this->db = $this->load->database("default");
		}
		if (!$this->logger) {
			$this->logger = $this->getLogger("model");
		}
	}
    
    /**
     * 返回日志对象
     */
    public function getLogger($name, $ident = '', $conf = array(), $level = PEAR_LOG_DEBUG)
    {
        $logger = Log::factory("composite", '', '', array(), PEAR_LOG_ERR);
        $conf = array(
            'lineFormat' => '[%1$s] [%3$s] %5$s(%6$s): %4$s',
            'timeFormat' => '%Y-%m-%d %H:%M:%S',
            'mode' => '0666'
        );
        $loggerDisplay = Log::factory('display', '', '', $conf, PEAR_LOG_ERR);
        $filename = "/data0/logs/shijian/{$name}_" . date('Ymd') . ".log";
        $loggerFile = Log::factory('file', $filename, '', $conf, PEAR_LOG_INFO);
        $logger->addChild($loggerDisplay);
        $logger->addChild($loggerFile);
        
        return $logger;
    }
	/**
     * 设置错误号和错误信息，用于某些需要返回具体错误原因的接口
     * @param int $errNo
     * @param string $errMsg
     * @return boolean
     */
    protected function _setError($errNo, $errMsg = '')
    {
        $this->errNo = $errNo;
        $this->errMsg = $errMsg;
        return true;
    }
    
    /**
     * 获取错误号
     */
    public function getErrNo()
    {
        return $this->errNo;
    }
    
    /**
     * 获取错误信息
     */
    public function getErrMessage()
    {
        return $this->errMsg;
    }
}